<template>
  <el-button
    @click.native.prevent="openDialog"
    :type="styleType"
    size="mini"
    :delApi="delApi"
    :disabled="disabled"
    :loading="loading"
    :handleParam="handleParam"
    :scope="scope"
    :btnUrl="btnUrl">
    {{ text }}
    <i :class="icon"></i>
  </el-button>
</template>
<script lang="ts">
  import {Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from 'vue-property-decorator'
  import { State } from "vuex-class"
  import { fmlCom } from "../utils";
  @Component({
    components:{}
  })
  export default class fmlEditBtn extends Vue{
    btnper:any={};//操作按钮权限
    loading:boolean=false;//操作按钮加载
    @Prop()
    text: string; //按钮名称
    @Prop()
    btnUrl:string; //按钮权限
    @Prop()
    resetPassApi:any; //重置密码接口
    @Prop()
    delApi: any; //删除接口
    @Prop()
    type:any;//按钮类型 查看look  修改edit 新增add  审核audit 删除del 重置密码reset
    @Prop()
    disabled:boolean;//是否禁用
    @Prop()
    styleType: string;//按钮样式
    @Prop()
    icon:string;//按钮上的图表
    @Prop()
    scope: any ;// 当前table行的数据
    @Prop()
    handleParam:object;

    //重置密码
    async resetPassword(param) {
      let [err] = await this.$fetchService.fetchGet(param,'resetPass',false);
      if (err) return this.$message({message:err.message || "重置密码失败",type:'error',showClose:true});
      this.$message({ type: "success", message: "重置成功",showClose:true });
      if (param.userId == this.$store.state.userInfo.userId) {
        sessionStorage.clear()
        this.$router.replace({ path: "/login" });
      } else {
        this.$store.commit("setUpdateTable");
      }
    }
    // 删除
    async delHandle(param) {
      const [err] = await this.$fetchService.fetchDelete(this.handleParam,this.delApi,true);
      if (err) {
        return this.$message({message:err.message || "操作失败",showClose:true,type:'error'});
      }
      this.$message({
        type: "success",
        message: "删除成功",
        showClose:true
      });
      this.$store.commit("setUpdateTable");
    }
    // 弹窗
    openDialog(e) {
      // 修改edit 新增add 审核audit 查看look 删除del 重置密码reset
      let type=['edit','add','audit','look','del','reset']
      if(type.indexOf(this.type)>-1&&this.type!='del'&&this.type!='reset'){
        this.$store.commit("setEditData", this.scope.row);
        this.$store.commit("setDialogType", this.type);
        this.$store.commit("setDialogVisible", true);
      }
      // 删除
      if(this.type=='del'&&!this.handleParam)return this.$message({type:'warning',message:'前台参数异常',showClose:true})
      if (this.type == "del"&&this.handleParam) {
        fmlCom.confirmTips(this,{
            title: "提示",
            message: "确定删除当前数据吗？删除后将无法恢复"
          }, btn => {
            if (btn == "confirm") {
              this.delHandle(this.handleParam);
            }
          }
        );
      }
      // 重置密码
      if(this.type=='reset'&&!this.handleParam)return this.$message({type:'warning',message:'前台参数异常',showClose:true})
      if (this.type === "reset"&&this.handleParam) {
        fmlCorm.confirmTips(this,{
            title: "提示",
            message: "确认重置密码吗？"
          },btn => {
            if (btn == "confirm") {
              this.resetPassword(this.handleParam);
            }
          }
        );
      }
    }
    created() {
      this.btnper = { url: this.$route.path, btnUrl: this.btnUrl };
    }
  }
</script>

